﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using ThingyCloudWPF.Creatures;

namespace ThingyCloudWPF.Graphics
{
	/// <summary>
	/// Interaction logic for AvatarGraphic.xaml
	/// </summary>
	public partial class AvatarGraphic : UserControl
	{
		private MainWindow mainwin;
		private Avatar avatar;

		public AvatarGraphic(MainWindow mainwin, Avatar a)
		{
			InitializeComponent();

			this.mainwin = mainwin;
			avatar = a;

			Loaded += new RoutedEventHandler(AvatarGraphic_Loaded);
		}

		public void AvatarGraphic_Loaded(object sender, RoutedEventArgs e)
		{
			string[] files;

			if (avatar.Weight == 1)
			{
				files = new string[] { "A1", "B1", "C1", "D1", "E1", "F1" };
			}
			else if (avatar.Weight == 2)
			{
				files = new string[] { "A2", "B2", "C2", "D2", "E2", "F2" };
			}
			else if (avatar.Weight == 3)
			{
				files = new string[] { "A3", "B3", "C3", "D3", "E3", "F3" };
			}
			else
			{
				files = new string[] { "C4", "D4", "E4", "F4" };
			}

			//string[] files = Directory.GetFiles("C:/dev/ThingyCloudWPF/ThingyCloudWPF/Images/Avatars/");
			Random r = new Random(DateTime.Now.Millisecond);
			graphic.Source = new BitmapImage(new Uri("file://C:/dev/ThingyCloudWPF/ThingyCloudWPF/Images/Avatars/" + files[r.Next(0, files.Length - 1)] + ".png"));
		}

		public int AvatarID
		{
			get
			{
				return avatar.ID;
			}
		}

		public bool IsGraphicFor(Avatar a)
		{
			return avatar.ID == a.ID;
		}

		public void Update()
		{
			/*
			Margin = new Thickness(
				mainwin.ToWindowX(Globals.ToAbsX(avatar.X) - (int)Math.Round((double)avatar.Width / 2)),
				mainwin.ToWindowY(Globals.ToAbsY(avatar.Y) - (int)Math.Round((double)avatar.Height / 2)),
				0,
				0);
			
			graphic.Width = avatar.Width;
			graphic.Height = avatar.Height;
			*/

			System.Drawing.Rectangle r = avatar.GetBoundingRect();

			Margin = new Thickness(
				mainwin.ToWindowX(r.X),
				mainwin.ToWindowY(r.Y),
				0,
				0);

			graphic.Width = r.Width;
			graphic.Height = r.Height;

			UpdateLayout();
		}
	}
}
